﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using Jzy.Core.Common.Web;
using System.Text.RegularExpressions;
using System.Text;
using Jzy.Workflow.Flow;
using Frame.Lib.Utilities;


namespace Jzy.Web.WorkFlow.Design
{
    public partial class Tab3 : System.Web.UI.Page
    {
        protected string FlowID
        {
            get
            {
                return Request["FlowID"];
            }
        }
        private FlowBO _flow = new FlowBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            FlowInfo info = _flow.GetInfo(FlowID);
            if (info != null)
            {
                XElement html = XElement.Parse(info.FormTemplate);
                head.Text = html.Element("head").Value;

                litForm.Text = ReplaceField(html.Element("body").Value, "input");

                StringBuilder body = new StringBuilder(html.Element("body").Value);
                this.EnableViewState = false;
               
            }

            JSUtil.SetDivHeight("formContent", 40);
        }
        private string ReplaceField(string src, string tagName)
        {

            Regex input = new Regex(@"<\s*(input|select|textarea)[^>]*?>", RegexOptions.IgnoreCase);

            return input.Replace(src,new MatchEvaluator(CustReplace));

            //input.Replace(src,new MatchEvaluator(

            //foreach (Match result in input.Matches(src.ToString()))
            //{
            //    string str = result.Value;//一个控件字符串

            //    if (str.Contains("button"))
            //    {
            //        continue;
            //    }

            //    Regex ctlName = new Regex("name\\s*=['\"]?(\\s*\\w*\\s*)['\" >]");

            //    string fieldName = ctlName.Match(str).Groups[1].Value;
            //    if (fieldName.Trim() != "")
            //    {
            //        target.Add(new XElement(fieldName));
            //    }
            //    else
            //    {
            //        int index = str.IndexOf(tagName) + tagName.Length;
            //        str = str.Insert(index, " name=\"" + tagName.ToLower() + "_" + ctlIndex + "\" ");
            //        target.Add(new XElement(tagName.ToLower() + "_" + ctlIndex));
            //        ctlIndex++;
            //        src.Replace(result.Value, str);
            //    }
            //}

        }

        private string CustReplace(Match match)
        {
            string str = match.Value;//一个控件字符串

            Regex ctlName = new Regex("name\\s*=['\"]?(\\s*\\w*\\s*)['\" >]");     
       
            string name = ctlName.Match(str).Groups[1].Value; ;
            string img = "<img alt='' src='../../Common/Images/Workflow/setup.gif' border='0' style='cursor:pointer;' onclick='setMenu(\"" + name + "\");' oncontextmenu='setMenu(\"" + name + "\");return false;'/>";
            return img;
        }
    }
}
